<?php 
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
class addondb
{
	private $conn;
	private $tablepre;
	private $result;
	private $sqliteAddonConfig;
	public function __construct($sqliteAddonConfig)
	{
		$this->sqliteAddonConfig=$sqliteAddonConfig;
		$this->conn = new SQLite3($this->sqliteAddonConfig['db']);
		$this->tablepre = $this->sqliteAddonConfig['prefix'];
		//Clear the config array, for security reasons.
		$dbConfig = array();
		unset($this->sqliteAddonConfig);
		if ($this->conn->lastErrorCode())
		{
			die("Error: database error:".$this->conn->lastErrorCode());
			return false;
		}
		require_once "./include/sql/db.sql.php";
	}
	public function __destruct()
	{
		$this->conn->close();
	}
	//Database CRUD operation functions
	/*	
	Parameters:
		String $table: the name of table
		Array $cols
	*/
	public function Ins($table,$cols)
	{
		$table = $this->tablepre.$table;
		$this->result = $this->conn->query(InsSql($table,$cols));
		if ($this->conn->lastErrorCode())
		{
			die("Error: database error:".$this->conn->lastErrorCode());
			return false;
		}
		if($this->result)
			return $this->conn->lastInsertRowID();
		return ;
	}
	public function GetOne($table,$cols,$order='id',$desc=false,$offset=0,$row=-1)
	{
		$results = $this->Get($table,$cols,$order='id',$desc=false,$offset=0,$row=-1);
		return $results[0];
	}
	public function Get($table,$cols,$order='id',$desc=false,$offset=0,$row=-1)
	{
		$table = $this->tablepre.$table;
		$this->result = $this->conn->query(GetSql($table,$cols,$order,$desc,$offset,$row));
		if ($this->conn->lastErrorCode())
		{
			die("Error: database error:".$this->conn->lastErrorCode());
			return false;
		}
		$results = array();
		while ($result = $this->result->fetchArray())
		{
			array_push ($results, $result);
		}
		if($results==array())
		{
			return ;
		}
		return $results;
	}
	public function GetWithLike($table,$cols,$like_cols,$order='id',$desc=false,$offset=0,$row=-1)
	{
		$table = $this->tablepre.$table;
		$this->result = $this->conn->query(GetWithLikeSql($table,$cols,$like_cols,$order,$desc,$offset,$row));
		if ($this->conn->lastErrorCode())
		{
			die("Error: database error:".$this->conn->lastErrorCode());
			return false;
		}
		$results = array();
		while ($result = $this->result->fetchArray())
		{
			array_push ($results, $result);
		}
		if(count($results)==1)
		{
			return $results[0];
		}
		else if($results==array())
		{
			return ;
		}
		return $results;
	}
	public function Set($table,$src_cols,$dest_cols)
	{
		$table = $this->tablepre.$table;
		$this->result = $this->conn->query(SetSql($table,$src_cols,$dest_cols));
		if ($this->conn->lastErrorCode())
		{
			die("Error: database error:".$this->conn->lastErrorCode());
			return false;
		}
		return $this->conn->changes();
	}
	public function Del($table,$cols)
	{
		$table = $this->tablepre.$table;
		return ;
	}
}
?>